var util = require('../../utils/util.js');
var Data = require("../../utils/data.js");
var api = require('../../config/api.js');
var app = getApp();

Page({
  data: {
    hotelList: [],
    date: '',
    tomorrow: '',
    dates: 1,
    city:"北京"
  },
  getData: function () {
    let that = this;
    util.request(api.HotelIndex).then(function (res) {
      if (res.code === '200') {
        that.setData({
          hotelList: res.datas.hotelList
        });
      }
    });
  },

  //事件处理函数
  bindViewTap: function () {
    var that = this;
    var startDate = that.data.date;
    var endDate = that.data.tomorrow;
    console.log(startDate);
    console.log(endDate);
    wx.navigateTo({
      url: '../calender/index?startDate=' + startDate + "&endDate=" + endDate
    })
  },

  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
   this.getData();
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    var startDate = this.data.startDate;
    var endDate = this.data.endDate;
    console.log(startDate);
    console.log(endDate);
    var date = Data.formatDate(new Date(), "yyyy-MM-dd");
    var tomorrow1 = new Date();
    tomorrow1.setDate((new Date()).getDate() + 1);
    var tomorrow = Data.formatDate(new Date(tomorrow1), "yyyy-MM-dd");
    if (startDate == null) {
      startDate = date;
      endDate = tomorrow;
    }
    var aDate, oDate1, oDate2, iDays
    aDate = endDate.split("-")
    oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])    //转换为12-18-2006格式  
    aDate = startDate.split("-")
    oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24)    //把相差的毫秒数转换为天数  
    this.setData({
      date: startDate,
      tomorrow: endDate,
      dates: iDays
    });
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  search:function(){
    wx.navigateTo({
      url: '/pages/hotelList/index',
    })
  },
   selectCity: function () {
    wx.navigateTo({
      url: '/pages/city/index'
    })
  }
})